package org.looom.orglooomschool;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.HashMap;
import java.util.Map;

public class MainActivity6 extends AppCompatActivity implements View.OnClickListener{
    private EditText tv_name;
    private EditText tv_phone;
    private Button add;
    private Button sel;
    private Button modify;
    private Button del;
    private TextView tv_show;

    private HashMap<String, String> info = new HashMap<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_6);
        tv_name = findViewById(R.id.tv_name);
        tv_phone = findViewById(R.id.tv_phone);
        add = findViewById(R.id.add);
        sel = findViewById(R.id.sel);
        modify = findViewById(R.id.modify);
        del = findViewById(R.id.del);
        tv_show = findViewById(R.id.tv_show);
        add.setOnClickListener(this);
        sel.setOnClickListener(this);
        modify.setOnClickListener(this);
        del.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.sel) {
            // 查询所有信息
            StringBuilder sb = new StringBuilder();
            for (Map.Entry<String, String> entry : info.entrySet()) {
                sb.append("姓名: ").append(entry.getKey()).append(", 电话: ").append(entry.getValue()).append("\n");
            }
            if (sb.length() == 0) {
                sb.append("没有保存任何信息");
            }
            tv_show.setText(sb.toString());
            Toast.makeText(this, "查询成功", Toast.LENGTH_LONG).show();
        }
        if (v.getId() == R.id.add) {
            // 添加信息
            String name = tv_name.getText().toString();
            String phone = tv_phone.getText().toString();
            if (TextUtils.isEmpty(name) || TextUtils.isEmpty(phone)) {
                Toast.makeText(this, "姓名和电话不能为空", Toast.LENGTH_LONG).show();
                return;
            }
            info.put(name, phone);
            Toast.makeText(this, "添加成功", Toast.LENGTH_LONG).show();
        }
        if (v.getId() == R.id.modify) {
            // 修改信息
            String name = tv_name.getText().toString();
            String phone = tv_phone.getText().toString();
            if (TextUtils.isEmpty(name) || TextUtils.isEmpty(phone)) {
                Toast.makeText(this, "姓名和电话不能为空", Toast.LENGTH_LONG).show();
                return;
            }
            if (info.containsKey(name)) {
                info.put(name, phone);
                Toast.makeText(this, "修改成功", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(this, "姓名不存在", Toast.LENGTH_LONG).show();
            }
        }
        if (v.getId() == R.id.del) {
            // 删除所有
            info.clear();
            tv_show.setText("");
            Toast.makeText(this, "删除成功", Toast.LENGTH_LONG).show();
        }
    }
}
